package com.vercon.moensmart.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.google.gson.Gson
import com.vercon.common.utils.DensityTool
import com.vercon.common.view.GridSpaceItemDecoration
import com.vercon.moensmart.adapter.FollowMemberAdapter
import com.vercon.moensmart.adapter.HealthTargetAdapter
import com.vercon.moensmart.adapter.base.BaseAdapter
import com.vercon.moensmart.databinding.FragmentHealthLayoutBinding
import com.vercon.moensmart.dialog.HealthDetailsDialog
import com.vercon.moensmart.fragment.base.BaseFragment
import com.vercon.moensmart.fragment.manager.FragmentOpenManager
import com.vercon.moensmart.mode.HealthMeasureTarget
import com.vercon.moensmart.mode.response.FollowMember
import com.vercon.moensmart.mode.response.HealthMeasure
import com.vercon.moensmart.viewmodel.SystemViewModel

class HealthFragment : BaseFragment<FragmentHealthLayoutBinding>() {


    var healthTargetAdapter : HealthTargetAdapter? = null
    var systemViewModel : SystemViewModel? = null
    var followMemberAdapter : FollowMemberAdapter? = null
    override fun onCreateViewBinding(inflater: LayoutInflater, parent: ViewGroup?): FragmentHealthLayoutBinding {
      return FragmentHealthLayoutBinding.inflate(inflater,parent,false)
    }

    override fun initData() {
        systemViewModel = ViewModelProvider(requireActivity())[SystemViewModel::class.java]

        binding?.healthRecyclerView?.layoutManager = GridLayoutManager(context,2)
        binding?.healthRecyclerView?.addItemDecoration(GridSpaceItemDecoration(2, DensityTool.dip2px(context, 20f),
            DensityTool.dip2px(context, 20f)))
        healthTargetAdapter = HealthTargetAdapter(context)
        binding?.healthRecyclerView?.adapter = healthTargetAdapter

        followMemberAdapter = FollowMemberAdapter(context)
        binding?.followMemberRecyclerView?.adapter = followMemberAdapter

    }

    override fun initListener() {

        systemViewModel?.followMemberLiveData?.observe(this@HealthFragment){value ->
            if(value != null){
                followMemberAdapter?.setData(value.list)
                systemViewModel?.currentUserIdLiveData?.value?.let { followMemberAdapter?.setCurrentId(it) }
                for ((i,bean) in value.list.withIndex()){
                    if(bean.pk.equals(systemViewModel?.currentUserIdLiveData?.value)){
                        binding?.tvCurrentDataUserName?.text = bean.nickname + "的数据"
                    }
                }
            }else{
                followMemberAdapter?.setData(ArrayList())
                followMemberAdapter?.setCurrentId("")
            }
        }
        systemViewModel?.healthMeasureLiveData?.observe(this@HealthFragment) { value ->
            try {
                binding?.tvNoData?.visibility = View.GONE
                val data = Gson().fromJson(value?.list?.get(0)?.log_content, HealthMeasure.LogContent::class.java)
                healthTargetAdapter!!.setData(HealthMeasureTarget.getShowList(data))

                binding?.tvValueTitle?.text = "去脂体重" + String.format("%.1f", data.fat_body_mass) + "公斤"
                binding?.tvValue?.text = String.format("%.1f", data.weight)
                binding?.tvValueUpdateTime?.text = "测量时间:" + value?.list?.get(0)?.create_time
            } catch (e: Exception) {
                e.printStackTrace()
                binding?.tvNoData?.visibility = View.VISIBLE
                healthTargetAdapter!!.setData(ArrayList())
                binding?.tvValueTitle?.text = "去脂体重--公斤"
                binding?.tvValue?.text = "--"
                binding?.tvValueUpdateTime?.text = "测量时间:--"
            }
        }

        followMemberAdapter?.setItemOnClickListener(object : BaseAdapter.BaseItemListener<FollowMember.ListDTO?>{
            override fun onItemClickListener(v: View?, data: FollowMember.ListDTO?, position: Int) {
                followMemberAdapter?.setCurrentId(data!!.pk)
                systemViewModel?.setCurrentUserIdLiveData(data?.pk)
                binding?.tvCurrentDataUserName?.text = data!!.nickname + "的数据"
                binding?.rlFollowMember?.visibility = View.GONE
            }
        })


        binding?.llToSeeFollowMember?.setOnClickListener {
            binding?.rlFollowMember?.visibility = View.VISIBLE
        }
        binding?.rlFollowMember?.setOnClickListener {
            binding?.rlFollowMember?.visibility = View.GONE
        }





        healthTargetAdapter?.setItemOnClickListener(object : BaseAdapter.BaseItemListener<HealthMeasureTarget>{
            override fun onItemClickListener(v: View?, data: HealthMeasureTarget, position: Int) {
                val dialog = HealthDetailsDialog()
                val bundle = Bundle()
                bundle.putString(HealthDetailsDialog.CURRENT_TAB,data.type)
                dialog.arguments = bundle
                dialog.show(childFragmentManager,"")
            }
        })

        binding?.igSeeWeightData?.setOnClickListener {
            val dialog = HealthDetailsDialog()
            val bundle = Bundle()
            bundle.putString(HealthDetailsDialog.CURRENT_TAB,HealthMeasureTarget.WEIGHT)
            dialog.arguments = bundle
            dialog.show(childFragmentManager,"")
        }

        binding?.igClose?.setOnClickListener {
            activity?.let { FragmentOpenManager.getInstance().closeFragment(it) }
        }
    }

    override fun onClick(v: View?) {

    }
}